package ch15
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.setTimeout;
    
    import utils.Ball3D;
    
    /**
     * 3D弹性运动
     * 点击鼠标将创建出一个随机的目标点。
     * @author jianping.shenjp
     */
    public class Spring3D extends Sprite
    {
        private var ball:Ball3D;
		//目标点
        private var tx:Number;
        private var ty:Number;
        private var tz:Number;
		//弹性系数
        private var spring:Number = .1;
		//摩擦系数
        private var friction:Number = .94;
		//焦距
        private var fl:Number = 250;
		//消失点 
        private var vpX:Number = stage.stageWidth / 2;
        private var vpY:Number = stage.stageHeight / 2;
        
        public function Spring3D()
        {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			setTimeout(init,500);
        }
        
        private function init():void
        {
            ball = new Ball3D();
            addChild(ball);
            tx = Math.random() * 500 - 250;
            ty = Math.random() * 500 - 250;
            tz = Math.random() * 500;
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }
        
        private function onEnterFrame(event:Event):void
        {
            var dx:Number = tx - ball.xpos;
            var dy:Number = ty - ball.ypos;
            var dz:Number = tz - ball.zpos;
            ball.vx += dx * spring;
            ball.vy += dy * spring;
            ball.vz += dz * spring;
            ball.xpos += ball.vx;
            ball.ypos += ball.vy;
            ball.zpos += ball.vz;
            ball.vx *= friction;
            ball.vy *= friction;
            ball.vz *= friction;
            if (ball.zpos > -fl)
            {
                var scale:Number = fl / (fl + ball.zpos);
                ball.scaleX = ball.scaleY = scale;
                ball.x = vpX + ball.xpos * scale;
                ball.y = vpY + ball.ypos * scale;
                ball.visible = true;
            }
            else
            {
                ball.visible = false;
            }
        }
        
        private function onMouseDown(event:MouseEvent):void
        {
            tx = Math.random() * 500 - 250;
            ty = Math.random() * 500 - 250;
            tz = Math.random() * 500;
        }
    }
}